package com.example.bmi;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class BmiActivity extends AppCompatActivity {
    private EditText etWeight, etHeight;
    private RadioGroup rgSex;
    private TextView tvBmi, tvInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etWeight = findViewById(R.id.et_weight);
        etHeight = findViewById(R.id.et_height);
        rgSex = findViewById(R.id.rg_sex);
        tvBmi = findViewById(R.id.tv_bmi);
        tvInfo = findViewById(R.id.tv_info);

        Button btnConfirm = findViewById(R.id.btn_confirm);
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取输入的值
                String weightStr = etWeight.getText().toString();
                String heightStr = etHeight.getText().toString();
                // 判断是否为空
                if (TextUtils.isEmpty(weightStr) || TextUtils.isEmpty(heightStr)) {
                    Toast.makeText(BmiActivity.this, "体重或身高不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                // 将字符串转为float，计算bmi
                float weight = Float.parseFloat(weightStr);
                float height = Float.parseFloat(heightStr);
                double bmi = (weight / Math.pow(height, 2));
                tvBmi.setText(String.format("BMI： %.2f", bmi));
                tvInfo.setText("诊断：" + getInfo(bmi));
                BmiActivity.hideKeyboard(BmiActivity.this);
            }
        });
    }

    private String getInfo(double bmi) {
        // 获取RadiButton的文本
        int id = rgSex.getCheckedRadioButtonId();
        RadioButton btn = findViewById(id);
        String sex = btn.getText().toString();

        // 根据bmi得到信息
        String info = "";
        boolean isMan = sex.equals("男");
        if (bmi > 35 && isMan || bmi > 34 && !isMan) {
            info = "重度肥胖";
        } else if (bmi >= 30 && isMan || bmi >= 29 && !isMan) {
            info = "中度肥胖";
        } else if (bmi >= 27 && isMan || bmi >= 26 && !isMan) {
            info = "轻度肥胖";
        } else if (bmi >= 25 && isMan || bmi >= 24 && !isMan) {
            info = "体重超重";
        } else if (bmi >= 20 && isMan || bmi >= 19 && !isMan) {
            info = "体重正常";
        } else {
            info = "体重过轻";
        }
        return info;
    }

    public static void hideKeyboard(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        View view = activity.getCurrentFocus();
        if (view == null) {
            view = new View(activity);
        }
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    public static void hideKeyboardFrom(Context context, View view) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}